package main

import (
	"distcompiler/network"
	"embed"
	"io"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

//go:embed cl.exe
var embedFs embed.FS

func main() {
	network.Discovery()

	file, err := os.OpenFile("e:/backup.log", os.O_WRONLY|os.O_CREATE, os.ModePerm)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	log.SetOutput(file)
	pathname, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	cmd := exec.Command("cl_.exe", os.Args[2:]...)
	cmd.Dir = pathname
	log.Print(os.Args)
	out, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}
	err = cmd.Start()
	if err != nil {
		log.Fatal(err)
	}

	defer out.Close()
	outBytes, err := io.ReadAll(out)
	if err != nil {
		log.Fatal(err)
	}
	if err := cmd.Wait(); err != nil {
		log.Fatal(err)
	}
	log.Print(string(outBytes))
}
